package org.ucas.faker.web.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.ucas.faker.web.entity.Record;

import java.util.List;

@Repository
public interface RecordDao extends JpaRepository<Record,Long>{

  @Modifying
  @Query("DELETE FROM  Record r WHERE r.depotId = ?1")
  public void clear(long depotId);

  @Query("SELECT r FROM Record r WHERE r.depotId = ?1 AND r.patentId = ?2")
  public Record findByDepotIdAndPatentId(long depotId, long patentId);

  @Modifying
  @Query("UPDATE Record r SET r.selected = :target WHERE r.depotId = :depotId AND r.patentId = :id")
  public void updateSelected(@Param(value = "depotId") long depotId,@Param(value = "id") long id, @Param(value = "target") int target);

  @Modifying
  @Query("UPDATE Record r SET r.selected = :target WHERE r.depotId = :depotId AND r.patentId in :ids")
  public void updateSelected(@Param(value = "depotId") long depotId, @Param(value = "ids") List<Long> ids, @Param(value = "target") int target);

  @Query("SELECT COUNT(*) FROM Record r WHERE r.depotId = :depotId AND r.selected = :target")
  public Long countSelect(@Param(value = "depotId") long depotId, @Param(value = "target") int target);

  @Modifying
  @Query("DELETE FROM Record r WHERE r.depotId = :depotId AND r.patentId in :patents")
  public void removeBatch(@Param(value = "depotId") long depotId, @Param(value = "patents") List<Long> patents);
  
  public Long countByDepotId(long depot_id);
}
